package com.sg.service.biz.pbff.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.exception.Assert;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.pbff.common.BffTagDto;
import com.sg.dto.biz.pbff.req.*;
import com.sg.dto.biz.pbff.res.*;
import com.sg.dto.integration.framework.*;
import com.sg.service.biz.pbff.TagService;
import com.wicket.okrframework.integration.BusinessObjectService;
import com.wicket.okrframework.integration.InductionRecordService;
import com.wicket.okrframework.integration.TagService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class TagServiceImpl implements TagService {

    @Resource
    com.sg.service.biz.framework.TagService fwBaseTagClient;
    @Resource
    com.sg.service.biz.framework.InductionRecordService fwBaseInductionRecordClient;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    com.sg.service.biz.framework.BusinessObjectService fwBaseBusinessObjectClient;

    /**
     * B1-1查询标签列表（平台）[608]
     * gen by moon at 11/3/2022, 11:14:56 PM
     */
    @Trace(operationName = "B1-1查询标签列表（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryTagListRespDto queryTagList(BffQueryTagListReqDto reqDto) {


        QueryTagListRespDto queryTagListRespDto_1 = null;
        BatchQueryInductionRecordComRespDto batchQueryInductionRecordComRespDto_1 = null;
//步骤0: D1-1查标签列表 - queryTagList
        QueryTagListRespDto queryTagListRespDto = null;
        QueryTagListReqDto queryTagListReqDto = new QueryTagListReqDto();
        if (reqDto != null) {
            queryTagListReqDto.setTagName(reqDto.getTagName());//SimpleFieldAssign//sourceId:26792_1
            queryTagListReqDto.setTagSource(reqDto.getTagSource());//SimpleFieldAssign//sourceId:26793_1
            queryTagListReqDto.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:388707_1
            queryTagListReqDto.setBusinessSceneCode(reqDto.getBusinessSceneCode());//SimpleFieldAssign//sourceId:388708_1
            queryTagListReqDto.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:26795_1
        }

        /*D1-1查标签列表[616]   */
        Assert.isNull(queryTagListReqDto.getIsArchive(), "B1-1查询标签列表（平台）-D1-1查标签列表-是否存档不能为空", false);
        queryTagListRespDto = fwBaseTagClient.queryTagList(queryTagListReqDto).getData();


        queryTagListRespDto_1 = queryTagListRespDto;

//步骤1: D1-3批量查询就职记录(管理)(公共) - batchQueryInductionRecordCom
        BatchQueryInductionRecordComRespDto batchQueryInductionRecordComRespDto = null;
        if (queryTagListRespDto != null) {
            BatchQueryInductionRecordComReqDto batchQueryInductionRecordComReqDto = new BatchQueryInductionRecordComReqDto();
            if (queryTagListRespDto != null) {
                batchQueryInductionRecordComReqDto.setOrgInductionRecordList(queryTagListRespDto.getTagList().stream().map(item -> item.getOperationInductionId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:419590_1
            }

            /*D1-3批量查询就职记录(管理)(公共)[5024]   */

            batchQueryInductionRecordComRespDto = fwBaseInductionRecordClient.batchQueryInductionRecordCom(batchQueryInductionRecordComReqDto).getData();


            batchQueryInductionRecordComRespDto_1 = batchQueryInductionRecordComRespDto;
        }

        BffQueryTagListRespDto retData = new BffQueryTagListRespDto();
        //数据集融合  MoreListToOneSource
        if (queryTagListRespDto_1 != null && !CollectionUtil.isEmpty(queryTagListRespDto_1.getTagList())) {
            for (TagDto tagDto : queryTagListRespDto_1.getTagList()) {
                BffTagDto retElm = new BffTagDto();
                if (queryTagListRespDto_1 != null) {
                    retElm.setTagId(tagDto.getTagId());//SimpleFieldAssign//sourceId:15696_2
                    retElm.setTagName(tagDto.getTagName());//SimpleFieldAssign//sourceId:15698_2
                    retElm.setTagSource(tagDto.getTagSource());//SimpleFieldAssign//sourceId:15699_2
                    retElm.setBusinessSceneCode(tagDto.getBusinessSceneCode());//SimpleFieldAssign//sourceId:101990_2
                    retElm.setIsRecommend(tagDto.getIsRecommend());//SimpleFieldAssign//sourceId:101991_2
                    retElm.setSubjectLifeCycle(tagDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:15702_2
                    retElm.setOperationInductionId(tagDto.getOperationInductionId());//SimpleFieldAssign//sourceId:15704_2
                    retElm.setOperateTime(tagDto.getOperateTime());//SimpleFieldAssign//sourceId:15705_2
                }
                retData.getTagList().add(retElm);
                if (batchQueryInductionRecordComRespDto_1 != null) {
                    for (OrgInductionRecordDto orgInductionRecordDto : batchQueryInductionRecordComRespDto_1.getOrgInductionRecordList()) {
                        if (orgInductionRecordDto.getInductionRecordId().equals(tagDto.getOperationInductionId())) {
                            if (batchQueryInductionRecordComRespDto_1 != null) {
                                retElm.setUserId(orgInductionRecordDto.getUserId());//SimpleFieldAssign//sourceId:115096_2
                                retElm.setUserFace(orgInductionRecordDto.getUserFace());//SimpleFieldAssign//sourceId:115097_2
                                retElm.setIdCardName(orgInductionRecordDto.getIdCardName());//SimpleFieldAssign//sourceId:115098_2
                                retElm.setNickName(orgInductionRecordDto.getNickName());//SimpleFieldAssign//sourceId:115099_2
                            }
                        }
                    }
                }

            }
        }//sourceId:34830_1


        retData.setPageNum(queryTagListRespDto.getPageNum());
        retData.setPageSize(queryTagListRespDto.getPageSize());
        retData.setTotal(queryTagListRespDto.getTotalNum());
        retData.setIsLastPage((Math.ceil(Double.valueOf(queryTagListRespDto.getTotalNum()) / Double.valueOf(queryTagListRespDto.getPageSize()))) <= retData.getPageNum());


        return retData;
    }

    /**
     * B1-1修改标签（平台）[611]
     * gen by moon at 10/24/2022, 9:56:05 PM
     */
    @Trace(operationName = "B1-1修改标签（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffUpdateTagRespDto updateTag(BffUpdateTagReqDto reqDto) {


        //步骤0: D1-1修改标签 - updateTag
        UpdateTagRespDto updateTagRespDto = null;
        UpdateTagReqDto updateTagReqDto = new UpdateTagReqDto();
        if (reqDto != null) {
            updateTagReqDto.setTagId(reqDto.getTagId());//SimpleFieldAssign//sourceId:35270_1
            updateTagReqDto.setTagName(reqDto.getTagName());//SimpleFieldAssign//sourceId:35271_1
            updateTagReqDto.setIsRecommend(reqDto.getIsRecommend());//SimpleFieldAssign//sourceId:349989_1
            updateTagReqDto.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:35273_1
        }

        /*D1-1修改标签[619]   */
        Assert.isNull(updateTagReqDto.getTagId(), "B1-1修改标签（平台）-D1-1修改标签-标签ID不能为空", false);
        updateTagRespDto = fwBaseTagClient.updateTag(updateTagReqDto).getData();


        BffUpdateTagRespDto retData = new BffUpdateTagRespDto();


        return retData;
    }

    /**
     * B1-1新增标签（平台）[610]
     * gen by moon at 10/24/2022, 9:56:04 PM
     */
    @Trace(operationName = "B1-1新增标签（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffAddTagRespDto addTag(BffAddTagReqDto reqDto) {


        AddTagRespDto addTagRespDto_1 = null;
//步骤0: D1-1新增标签 - addTag
        AddTagRespDto addTagRespDto = null;
        AddTagReqDto addTagReqDto = new AddTagReqDto();
        addTagReqDto.setTagSource("MANAGE_CREATE");//sourceId:34847_1
        if (reqDto != null) {
            addTagReqDto.setTagName(reqDto.getTagName());//SimpleFieldAssign//sourceId:35268_1
            addTagReqDto.setIsRecommend(reqDto.getIsRecommend());//SimpleFieldAssign//sourceId:349977_1
            addTagReqDto.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:388700_1
            addTagReqDto.setBusinessSceneCode(reqDto.getBusinessSceneCode());//SimpleFieldAssign//sourceId:349978_1
            addTagReqDto.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:34793_1
        }

        /*D1-1新增标签[618]   */
        Assert.isNull(addTagReqDto.getTagName(), "B1-1新增标签（平台）-D1-1新增标签-标签名称不能为空", false);
        Assert.isNull(addTagReqDto.getIsRecommend(), "B1-1新增标签（平台）-D1-1新增标签-是否推荐不能为空", false);
        Assert.isNull(addTagReqDto.getTagSource(), "B1-1新增标签（平台）-D1-1新增标签-标签来源方式不能为空", false);
        Assert.isNull(addTagReqDto.getSubjectLifeCycle(), "B1-1新增标签（平台）-D1-1新增标签-主体生命周期不能为空", false);
        addTagRespDto = fwBaseTagClient.addTag(addTagReqDto).getData();


        addTagRespDto_1 = addTagRespDto;

        BffAddTagRespDto retData = new BffAddTagRespDto();
        if (addTagRespDto_1 != null) {
            retData.setTagId(addTagRespDto_1.getTagId());//SimpleFieldAssign//sourceId:34798_1
        }


        return retData;
    }

    /**
     * B1-1查询标签详情（平台）[609]
     * gen by moon at 10/28/2022, 11:08:53 PM
     */
    @Trace(operationName = "B1-1查询标签详情（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryTagDetailRespDto queryTagDetail(BffQueryTagDetailReqDto reqDto) {


        QueryTagDetailRespDto queryTagDetailRespDto_1 = null;
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto_1 = null;
//步骤0: D1-1查询标签详情 - queryTagDetail
        QueryTagDetailRespDto queryTagDetailRespDto = null;
        QueryTagDetailReqDto queryTagDetailReqDto = new QueryTagDetailReqDto();
        if (reqDto != null) {
            queryTagDetailReqDto.setTagId(reqDto.getTagId());//SimpleFieldAssign//sourceId:26770_1
        }

        /*D1-1查询标签详情[617]   */
        Assert.isNull(queryTagDetailReqDto.getTagId(), "B1-1查询标签详情（平台）-D1-1查询标签详情-标签ID不能为空", false);
        queryTagDetailRespDto = fwBaseTagClient.queryTagDetail(queryTagDetailReqDto).getData();
        Assert.isTrue(queryTagDetailRespDto == null || queryTagDetailRespDto.getTagId() == null, "返回值不能为空", false);

        queryTagDetailRespDto_1 = queryTagDetailRespDto;

//步骤1: D1-3查询个人就职记录信息详情（公共） - queryInductionRecordInfoDetailCom
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto = null;
        if (queryTagDetailRespDto != null) {
            QueryInductionRecordInfoDetailComReqDto queryInductionRecordInfoDetailComReqDto = new QueryInductionRecordInfoDetailComReqDto();
            if (queryTagDetailRespDto != null) {
                queryInductionRecordInfoDetailComReqDto.setInductionRecordId(queryTagDetailRespDto.getOperationInductionId());//SimpleFieldAssign//sourceId:16557_1
            }

            /*D1-3查询个人就职记录信息详情（公共）[715]   */
            Assert.isNull(queryInductionRecordInfoDetailComReqDto.getInductionRecordId(), "B1-1查询标签详情（平台）-D1-3查询个人就职记录信息详情（公共）-就职记录ID不能为空", false);
            queryInductionRecordInfoDetailComRespDto = fwBaseInductionRecordClient.queryInductionRecordInfoDetailCom(queryInductionRecordInfoDetailComReqDto).getData();


            queryInductionRecordInfoDetailComRespDto_1 = queryInductionRecordInfoDetailComRespDto;
        }

        BffQueryTagDetailRespDto retData = new BffQueryTagDetailRespDto();
        if (queryTagDetailRespDto_1 != null) {
            retData.setTagId(queryTagDetailRespDto_1.getTagId());//SimpleFieldAssign//sourceId:26780_1
            retData.setTagName(queryTagDetailRespDto_1.getTagName());//SimpleFieldAssign//sourceId:26781_1
            retData.setTagSource(queryTagDetailRespDto_1.getTagSource());//SimpleFieldAssign//sourceId:26782_1
            retData.setBizAppCode(queryTagDetailRespDto_1.getBizAppCode());//SimpleFieldAssign//sourceId:406138_1
            retData.setBusinessSceneCode(queryTagDetailRespDto_1.getBusinessSceneCode());//SimpleFieldAssign//sourceId:349971_1
            retData.setIsRecommend(queryTagDetailRespDto_1.getIsRecommend());//SimpleFieldAssign//sourceId:349972_1
            retData.setSubjectLifeCycle(queryTagDetailRespDto_1.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:26783_1
            retData.setOperationInductionId(queryTagDetailRespDto_1.getOperationInductionId());//SimpleFieldAssign//sourceId:26785_1
            retData.setOperateTime(queryTagDetailRespDto_1.getOperateTime());//SimpleFieldAssign//sourceId:26786_1
        }
        if (queryInductionRecordInfoDetailComRespDto_1 != null) {
            retData.setUserId(queryInductionRecordInfoDetailComRespDto_1.getUserId());//SimpleFieldAssign//sourceId:34848_1
            retData.setIdCardName(queryInductionRecordInfoDetailComRespDto_1.getIdCardName());//SimpleFieldAssign//sourceId:26789_1
            retData.setNickName(queryInductionRecordInfoDetailComRespDto_1.getNickName());//SimpleFieldAssign//sourceId:26790_1
            retData.setUserFace(queryInductionRecordInfoDetailComRespDto_1.getUserFace());//SimpleFieldAssign//sourceId:26791_1
        }


        return retData;
    }

    /**
     * B1-1删除标签(管理)[5100]
     * gen by moon at 11/23/2022, 10:44:08 PM
     */
    @Trace(operationName = "B1-1删除标签(管理)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffDeleteTagRespDto deleteTag(BffDeleteTagReqDto reqDto) {


        //步骤0: D1-1删除标签 - deleteTag
        DeleteTagRespDto deleteTagRespDto = null;
        DeleteTagReqDto deleteTagReqDto = new DeleteTagReqDto();
        if (reqDto != null) {
            deleteTagReqDto.setTagId(reqDto.getTagId());//SimpleFieldAssign//sourceId:417789_1
        }

        /*D1-1删除标签[620]   */
        Assert.isNull(deleteTagReqDto.getTagId(), "B1-1删除标签(管理)-D1-1删除标签-标签ID不能为空", false);
        deleteTagRespDto = fwBaseBusinessObjectClient.deleteTag(deleteTagReqDto).getData();


        BffDeleteTagRespDto retData = new BffDeleteTagRespDto();


        return retData;
    }
    //
}
